if (SERVER) then
	AddCSLuaFile("shared.lua");
end;

if (CLIENT) then
	SWEP.PrintName			= "Zipties";
	SWEP.Instructions		 = "Left click to tie.\nRight click to untie.";

	SWEP.Slot			= 1;
	SWEP.SlotPos		= 2;
	SWEP.ViewModelFlip 	= false;
	SWEP.DrawAmmo 		= false;
	SWEP.ViewModelFOV	= 62;
end;

SWEP.Base = "leaf_weapon_base";

SWEP.AdminSpawnable		= true;

SWEP.WorldModel 		= Model("models/weapons/w_fists.mdl");
SWEP.ViewModel			= Model("models/weapons/v_fists.mdl");

SWEP.Primary.ClipSize		= 1;
SWEP.Primary.DefaultClip	= 1;
SWEP.Primary.Automatic		= false;
SWEP.Primary.Ammo			= "none";

SWEP.Secondary.ClipSize		= 1;
SWEP.Secondary.DefaultClip	= 1;
SWEP.Secondary.Automatic	= false;
SWEP.Secondary.Ammo			= "none";

SWEP.HoldType 	 		= "fist";

function SWEP:Deploy()
	if (SERVER) then
		self.Owner:DrawViewModel(false);
		self.Owner:DrawWorldModel(false);
	end;
end;

function SWEP:Reload()
end;

function SWEP:CustomThink()
	if (SERVER) then
		if (self.Owner.Untying) then
			if (!self.Owner:KeyDown(IN_ATTACK2)) then
				self.Owner.Untying = false;
				self.Owner.UntieTarget:SetNWBool("leaf_btied", false);
				self.Owner:SetNWBool("leaf_tying", false);
				
				umsg.Start("leaf_runtie", self.Owner)
				umsg.End();
			else
				local tr = self.Owner:EyeTrace(99999999);
				local entity;
				
				if (IsValid(tr.Entity)) then
					if (tr.Entity:IsRagdoll()) then
						entity = self.Owner.UntieTarget;
					else
						entity = tr.Entity;
					end;
				end;
				
				if (tr.HitWorld or IsValid(entity) and entity != self.Owner.UntieTarget or self.Owner:GetPos():Distance(entity:GetPos()) > 120) then
					self.Owner.Untying = false;
					self.Owner.UntieTarget:SetNWBool("leaf_btied", false);
					self.Owner:SetNWBool("leaf_tying", false);
					
					umsg.Start("leaf_runtie", self.Owner)
					umsg.End();
				end;
				
				if (self.Owner.UntieTime +5 < CurTime()) then
					self.Owner.Untying = false;
					self.Owner.UntieTarget:SetNWBool("leaf_tied", false);
					self.Owner.UntieTarget:SetNWBool("leaf_btied", false);
					
					self.Owner:SetNWBool("leaf_tying", false);
					
					self.Owner.UntieTarget = nil;
					
					umsg.Start("leaf_runtie", self.Owner)
					umsg.End();
				end;
			end;
		end;
		
		if (self.Owner.Tying) then
			if (!self.Owner:KeyDown(IN_ATTACK)) then
				self.Owner.Tying = false;
				self.Owner.TieTarget:SetNWBool("leaf_butied", false);
				self.Owner:SetNWBool("leaf_tying", false);
				
				umsg.Start("leaf_rtie", self.Owner)
				umsg.End();
			else
				local tr = self.Owner:EyeTrace(99999999);
				local entity;
				
				if (IsValid(tr.Entity)) then
					if (tr.Entity:IsRagdoll()) then
						entity = self.Owner.TieTarget;
					else
						entity = tr.Entity;
					end;
				end;
				
				if (tr.HitWorld or IsValid(entity) and entity != self.Owner.TieTarget or self.Owner:GetPos():Distance(entity:GetPos()) > 120) then
					self.Owner.Tying = false;
					self.Owner.TieTarget:SetNWBool("leaf_butied", false);
					self.Owner:SetNWBool("leaf_tying", false);
					
					umsg.Start("leaf_rtie", self.Owner)
					umsg.End();
				end;
				
				if (self.Owner.TieTime +5 < CurTime()) then
					self.Owner.Tying = false;
					self.Owner.TieTarget:SetNWBool("leaf_tied", true);
					self.Owner.TieTarget:SetNWBool("leaf_butied", false);
					self.Owner:SetNWBool("leaf_tying", false);
					
					self.Owner.TieTarget = nil;
					
					umsg.Start("leaf_rtie", self.Owner)
					umsg.End();
				end;
			end;
		end;
	end;
end;

function SWEP:PrimaryAttack()
	self.Weapon:SetNextPrimaryFire(CurTime() +0.2);

	if (self.Owner:IsTied()) then return; end;
	
	if (SERVER) then
		local tr = self.Owner:EyeTrace(90);
		
		if (IsValid(tr.Entity)) then
			local valid;
			
			if (tr.Entity:IsRagdoll() and tr.Entity:GetRagdollPlayer()) then
				valid = tr.Entity:GetRagdollPlayer();
			elseif (tr.Entity:IsPlayer()) then
				valid = tr.Entity;
			end;
			
			if (IsValid(valid) and !valid:IsTied()) then
				self.Owner.Tying = true;
				self.Owner.TieTime = CurTime();
				self.Owner.TieTarget = valid;
				
				self.Owner:SetNWBool("leaf_tying", true);
				valid:SetNWBool("leaf_butied", true);
				
				umsg.Start("leaf_tie", self.Owner)
				umsg.End();
			end;
		end;
	end;
end;

function SWEP:SecondaryAttack()
	self.Weapon:SetNextSecondaryFire(CurTime() +0.2);
	if (self.Owner:IsTied()) then return; end;
	
	if (SERVER) then
		local tr = self.Owner:EyeTrace(90);
		
		if (IsValid(tr.Entity)) then
			local valid;
			
			if (tr.Entity:IsRagdoll() and tr.Entity:GetRagdollPlayer()) then
				valid = tr.Entity:GetRagdollPlayer();
			elseif (tr.Entity:IsPlayer()) then
				valid = tr.Entity;
			end;
			
			if (IsValid(valid) and valid:IsTied()) then
				self.Owner.Untying = true;
				self.Owner.UntieTime = CurTime();
				self.Owner.UntieTarget = valid;
				
				self.Owner:SetNWBool("leaf_tying", true);
				valid:SetNWBool("leaf_btied", true);
			
				umsg.Start("leaf_untie", self.Owner)
				umsg.End();
			end;
		end;
	end;
end;